package org.liang.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.liang.base.annotation.RonghuaPermission;
import org.liang.base.utils.AjaxResult;
import org.liang.base.utils.PageInfo;
import org.liang.system.domain.Department;
import org.liang.system.query.DepartmentQuery;
import org.liang.system.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/department")
@Api(value = "部门管理接口",description = "部门管理")
@RonghuaPermission(name = "部门管理",desc = "部门相关接口")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    // 新增/编辑
    @ApiOperation(value = "新增/修改部门")
    @PutMapping
    @RonghuaPermission(name = "部门保存",desc = "部门新增和修改")
    public AjaxResult save(@RequestBody Department department){
        try {
            if(Objects.isNull(department.getId())){
                departmentService.insert(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    // 删
    @ApiOperation(value = "删除部门")
    @DeleteMapping("/{id}")
    @RonghuaPermission(name = "部门删除",desc = "删除指定部门")
    public AjaxResult delete(@PathVariable("id") Serializable id){
        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }
    // 批量删
    @ApiOperation(value = "批量删除部门")
    @PatchMapping
    @RonghuaPermission(name = "部门删除",desc = "批量删除部门")
    public AjaxResult batchDelete(@RequestBody List<Serializable> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 查
    @ApiOperation(value = "查询指定id部门")
    @GetMapping("/{id}")
    @RonghuaPermission(name = "部门查询",desc = "查询指定部门")
    public AjaxResult selectById(@PathVariable("id") Serializable id){
        try {
            Department department = departmentService.selectById(id);
            return AjaxResult.success(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

    }
    // 查所有
    @ApiOperation(value = "查询所有部门")
    @GetMapping
    @RonghuaPermission(name = "部门查询",desc = "查询所有部门")
    public AjaxResult selectAll(){
        try {
            List<Department> departments = departmentService.selectAll();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //分页查询
    @PostMapping
    @RonghuaPermission(name = "部门查询",desc = "分页查询部门")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo<Department> pageInfo = departmentService.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 查部门上下级
    @ApiOperation(value = "查询查部门上下级")
    @GetMapping("/deptTree")
    @RonghuaPermission(name = "部门查询",desc = "查询部门树")
    public AjaxResult deptTree(){
        try {
            List<Department> deptTree = departmentService.deptTree();
            return AjaxResult.success(deptTree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 查部门上下级
    @ApiOperation(value = "查询查部门上下级")
    @GetMapping("/deptTreeTh")
    @RonghuaPermission(name = "部门查询",desc = "查询三级部门树")
    public AjaxResult deptTreeTh(){
        try {
            List<Department> deptTreeTh = departmentService.deptTreeTh();
            return AjaxResult.success(deptTreeTh);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


}
